package com.ms.game.invitefriend.async;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;

import com.ms.game.invitefriend.db.DistributorInviteCodeDAO;
import com.ms.game.invitefriend.db.DistributorsModel;
import com.ms.game.invitefriend.db.SubDistributorsModel;
import com.ms.game.invitefriend.message.SGetSubDistributorsInfoResponse;
import com.ms.game.invitefriend.message.SubDistributorInfo;
import com.ms.player.Player;
import com.ms.player.PlayerIdentityType;
import com.ms.service.error.ErrorCode;
import com.xpec.c4.db.dao.PlayerDAO;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.service.message.MessageAction;
import com.xpec.c4.service.module.task.TaskAsyncCommand;

public class GetSubDistributorsInfoAsync extends TaskAsyncCommand{

	/**
	 * 經銷商角色
	 */
	public Player player;
	
	/**
	 * 頁數
	 */
	public int page;
	
	/**
	 * 一頁幾筆
	 */
	public int maxPerPage;
	
	/**
	 * 
	 */
	public int messageType;
	
	/**
	 * sessionId
	 */
	public GUID sessionId;
	
	@Override
	public void asyncExecute() throws Exception {
		SGetSubDistributorsInfoResponse response = new SGetSubDistributorsInfoResponse();
		
		
		String playerId = player.getGuid().toString();

		//檢查是不是經銷商
		DistributorsModel distributorsModel = DistributorInviteCodeDAO.getDistributorModelByGUID(playerId);
		if(distributorsModel == null || !distributorsModel.isEffective()) {
			response.setErrorCode(ErrorCode.DATA_NOT_FOUND);
			MessageAction.sendMessage(sessionId, messageType, response);
			return;
		}
		
		int beginPos = (page - 1) * maxPerPage;
		if (page == 1) {
			// 第一次要給他總頁數
			int totalPage = DistributorInviteCodeDAO.getTotalSubDistributorsPage(playerId, maxPerPage);
			response.setTotalPage(totalPage);
		}
		
		//子經銷商的models
		ArrayList<SubDistributorsModel> subDistributorsModels = DistributorInviteCodeDAO.getSubDistributorsByPage(playerId, beginPos, maxPerPage);
		LinkedList<String> guids = new LinkedList<String>();
		for (SubDistributorsModel subDistributorsModel : subDistributorsModels) {
			guids.addLast(subDistributorsModel.getSubDistributorGuid());
		}
		
		//取角色等級map
		HashMap<String, Integer> playerLevelMap = PlayerDAO.getPlayerGuidLevelMap(guids);
		
		//取子經銷商使用次數的
		HashMap<String, Integer> playerUsedCountMap = DistributorInviteCodeDAO.getSubDistributorGuidUsedInviteCodeCountMap(guids);

		LinkedList<SubDistributorInfo> subDistributorInfos = new LinkedList<SubDistributorInfo>();
		for (SubDistributorsModel model : subDistributorsModels) {
			SubDistributorInfo info = new SubDistributorInfo();
			String subGuid = model.getSubDistributorGuid();
			
			info.setName(model.getSubDistributorName());
			info.setPlayerId(subGuid);
			info.setInvteCodeCount(model.getInviteCodeNum());
			
			int levelUpMoneyComm = model.getLevelUpMoneyComm() > distributorsModel.getLevelUpMoney()? distributorsModel.getLevelUpMoney() : model.getLevelUpMoneyComm() ;
			info.setLevelUpMoneyComm(levelUpMoneyComm);
			
			int designatedLeveMoneyComm = model.getDesignatedLeveMoneyComm() > distributorsModel.getDesignatedLeveMoney() ? distributorsModel.getDesignatedLeveMoney() : model.getDesignatedLeveMoneyComm();
			info.setDesignatedLeveMoneyComm(designatedLeveMoneyComm);
		
			int modifyLevelUpMoneyComm = model.getModifyLevelUpMoneyComm() > distributorsModel.getLevelUpMoney() ? distributorsModel.getLevelUpMoney() :  model.getModifyLevelUpMoneyComm();
			info.setModifyLevelUpMoneyComm(modifyLevelUpMoneyComm);
			
			int modifyDesignatedLeveMoneyComm = model.getModifyDesignatedLeveMoneyComm() > distributorsModel.getDesignatedLeveMoney() ?  distributorsModel.getDesignatedLeveMoney():model.getModifyDesignatedLeveMoneyComm();
			info.setModifyDesignatedLeveMoneyComm(modifyDesignatedLeveMoneyComm);
				
			info.setModify(model.isModify());
			if(playerUsedCountMap.containsKey(subGuid)){
				info.setUsedCount(playerUsedCountMap.get(subGuid));
			}
			
			if(playerLevelMap.containsKey(subGuid)){
				info.setLevel(playerLevelMap.get(subGuid));
			}
			
			subDistributorInfos.add(info);
		}
		
		//取得經銷商剩餘的經銷商邀請碼 
		int leftInviteCodes = DistributorInviteCodeDAO.getLeftDistributorInviteCodeCount(playerId , PlayerIdentityType.DISTRIBUTOR);
		
		response.setDesignatedLeveMoneyComm(distributorsModel.getDesignatedLeveMoney());
		response.setInviteCodeNum(leftInviteCodes);
		response.setLevelUpMoneyComm(distributorsModel.getLevelUpMoney());
		response.setSubDistributors(subDistributorInfos);
		response.setErrorCode(ErrorCode.SUCCESS);
		MessageAction.sendMessage(sessionId, messageType, response);
	}

	@Override
	public void execute() throws Exception {
		
	}
	
	
}
